package com.victor.zjbj_android.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;


import com.victor.zjbj_android.bean.LoginConfig;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;

/**
 * Created by liuwei on 2017/10/23.
 * 获取登录类信息的工具类
 */
public class LoginUtils {
    private static String TAG = "LoginUtils";

    /**
     * @Date 10.17
     * getResId("
     * ", R.drawable.class);
     **/
    public static int getResId(String variableName, Class<?> c) {
        try {
            Field idField = c.getDeclaredField(variableName);
            return idField.getInt(idField);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 序列化对象
     *
     * @param person
     * @return
     * @throws IOException
     */
    public static String serialize(LoginConfig person) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                byteArrayOutputStream);
        objectOutputStream.writeObject(person);
        String serStr = byteArrayOutputStream.toString("ISO-8859-1");
        serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
        objectOutputStream.close();
        byteArrayOutputStream.close();
        Log.e(TAG, "serialize str =" + serStr);
        return serStr;
    }

    /**
     * 反序列化对象
     *
     * @param str
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static LoginConfig deSerialization(String str) throws IOException,
            ClassNotFoundException {
        String redStr = java.net.URLDecoder.decode(str, "UTF-8");
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                redStr.getBytes("ISO-8859-1"));
        ObjectInputStream objectInputStream = new ObjectInputStream(
                byteArrayInputStream);
        LoginConfig person = (LoginConfig) objectInputStream.readObject();
        objectInputStream.close();
        byteArrayInputStream.close();
        return person;
    }

    public static void setLoginConfig(Context context, String strObject) {
        SharedPreferences sp = context.getSharedPreferences("loginConfig", 0);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("loginConfig", strObject);
        edit.commit();
    }


    private static String getShareLogin(Context context) {
        SharedPreferences sp = context.getSharedPreferences("loginConfig", 0);
        return sp.getString("loginConfig", "");
    }

    public static LoginConfig getLoginConfig(Context context) {
        try {
            return deSerialization(getShareLogin(context));
        } catch (IOException e) {
            //ToastUtils.shortToast(context,"获取登录数据失败！");
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
